<template>
  <!-- 三级菜单界面 -->
  <Category :scene="scene"></Category>
  <el-card style="margin: 10px 0px">
    <div v-show="scene === 0">
      <el-button
        type="primary"
        size="defalut"
        icon="Plus"
        :disabled="categoryStore.c3Id ? false : true"
        @click="handleAddSpu"
      >
        添加SPU
      </el-button>
      <el-table border style="margin: 10px 0px" :data="spuDataList">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="SPU名称" prop="spuName"></el-table-column>
        <!--                                                 描述文字过多时，多余文字暂时隐藏 -->
        <el-table-column
          label="SPU描述"
          prop="description"
          show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="操作">
          <!-- eslint-disable -->
          <!-- 不用#的话el-button不显示 -->
          <template #="{ row, $index }">
            <el-button
              type="primary"
              size="small"
              icon="Plus"
              title="添加SKU"
              @click="handleAddSku(row)"
            ></el-button>
            <el-button
              type="primary"
              size="small"
              icon="Edit"
              title="修改SPU"
              @click="handleEditSpu(row)"
            ></el-button>
            <el-button
              type="primary"
              size="small"
              icon="View"
              title="查看SKU列表"
              @click="handleSkuInfo(row)"
            ></el-button>
            <el-popconfirm :title="`你确定要删除${row.spuName}吗?`" @confirm="handleDeleteSpu(row)">
              <template #reference>
                <el-button type="primary" size="small" icon="Delete" title="删除SPU"></el-button>
              </template>
            </el-popconfirm>
          </template>
          <!-- eslint-enable -->
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :page-sizes="[3, 5, 7, 9]"
        :background="true"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
        @current-change="changePageNo"
        @size-change="sizeChange"
      />
    </div>
    <div v-show="scene === 1">
      <SpuForm ref="spu" @changeScene="changeScene" />
    </div>
    <div v-show="scene === 2">
      <SkuForm ref="sku" @changeScene="changeScene" />
    </div>
    <!-- 显示Sku信息的dialog组件 -->
    <el-dialog v-model="show" title="SKU列表">
      <el-table border :data="skuData">
        <el-table-column label="Sku名字" prop="skuName"></el-table-column>
        <el-table-column label="Sku价格" prop="price"></el-table-column>
        <el-table-column label="Sku重量" prop="weight"></el-table-column>
        <!-- eslint-disable -->
        <el-table-column label="Sku图片">
          <template #="{ row, $index }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
        <!-- eslint-enable -->
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
//引入categoryStore
import useCategoryStore from '@/store/modules/category'
//引入接口
import { reqHasSpu, reqHasSku, reqDeleteSpu } from '@/api/product/spu'
//引入element-plus的ElMessage模块
import { ElMessage } from 'element-plus'
//引入ts数据类型
import { SpuDataList, SpuData, SkuData } from '@/api/product/spu/type'
import { ref, watch, onBeforeUnmount } from 'vue'
//引入模块
//@ts-ignore
import SkuForm from './skuForm.vue'
//@ts-ignore
import SpuForm from './spuForm.vue'

//categoryStore
let categoryStore = useCategoryStore()
//场景数据
let scene = ref<number>(0)
//当前页数
let pageNo = ref<number>(1)
//页面展示数据数
let pageSize = ref<number>(3)
//存储当前SPU数据
let spuDataList = ref<SpuDataList>()
//存储当前SPU个数
let total = ref<number>(0)
//存储spuForm的vc实例
let spu = ref<any>()
//存储skuForm的vc实例
let sku = ref<any>()
//控制dialog是否显示
let show = ref<boolean>(false)
//存储获取的SKU数据
let skuData = ref<SkuData[]>([])

//监听c3Id，发生变化则进行一次获取数据请求
watch(
  () => categoryStore.c3Id,
  () => {
    if (!categoryStore.c3Id) return
    //获取已有SPU数据
    getSpu()
  },
)
//获取现有SPU数据
const getSpu = async () => {
  let result = await reqHasSpu(pageNo.value, pageSize.value, categoryStore.c3Id)
  if (result.code === 200) {
    spuDataList.value = result.data.spuDataList
    total.value = result.data.total
  } else {
    ElMessage({
      type: 'error',
      message: '获取SPU数据失败',
    })
  }
}
//页数改变时调用，根据当前pageNo和limit再请求一次数据
const changePageNo = () => {
  getSpu()
}
//改变每页显示产品个数时，同上
const sizeChange = () => {
  pageNo.value = 1
  getSpu()
}
//修改场景到添加/修改Spu
const handleAddSpu = () => {
  scene.value = 1
  spu.value.initAddSpuData(categoryStore.c3Id)
}
const handleEditSpu = async (row: SpuData) => {
  scene.value = 1
  //在子组件中调用事件initHasSpuData
  spu.value.initHasSpuData(row)
}
//传入spuForm的函数
const changeScene = (obj: any) => {
  scene.value = obj.value
  if (obj.params === 'Add') {
    pageNo.value = 1
  }
  getSpu()
}
//修改场景到查看Sku 添加Sku
const handleAddSku = (row: SpuData) => {
  scene.value = 2
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}
//查看某个Spu对应的Sku属性
const handleSkuInfo = async (row: SpuData) => {
  let result = await reqHasSku(row.id)
  console.log(result.data)
  if (result.code === 200) {
    skuData.value = result.data
    show.value = true
  } else {
    ElMessage({
      type: 'error',
      message: '获取SKU数据失败',
    })
  }
}
//删除某个Spu
const handleDeleteSpu = async (row: SpuData) => {
  let result = await reqDeleteSpu(row.id)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getSpu()
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
onBeforeUnmount(() => {
  categoryStore.$reset()
})
</script>

<style scoped lang="scss"></style>
